package com.zl.tby.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyCameraInfoQuery;
import com.zl.tby.dto.TbyUavInfoQuery;
import com.zl.tby.model.TbyUavInfo;
import com.zl.tby.service.ITbyUavInfoService;
import com.zl.tby.vo.TbyUavInfoForm;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 无人机信息 控制器
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/uav")
public class TbyUavInfoController {

    @Inject
    private ITbyUavInfoService tbyUavInfoService;

    /**
     * 保存或新增
     * @param form: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save", method = MethodType.POST)
    public R<TbyUavInfo> saveOrUpdate(@Validated TbyUavInfoForm form) {
        try {
            if (form.getId() == null) {
                return tbyUavInfoService.add(form);
            }
            return tbyUavInfoService.update(form);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return tbyUavInfoService.delete(id) == 1 ? R.ok() : R.failed("删除失败");
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyUavInfo> get(@NotNull Long id) {
        TbyUavInfo entity = this.tbyUavInfoService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 分页查询
     * @param query: 查询参数
     * @return 分页结果
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyUavInfoQuery query) {
        return R.ok(this.tbyUavInfoService.page(query));
    }

    /**
     * 列表查询
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list", method = MethodType.GET)
    public R<List<TbyUavInfo>> List(@Body TbyUavInfoQuery query) {
        return R.ok(this.tbyUavInfoService.list(query));
    }


    /**
     * 验证无人机编码是否存在
     * @param query: 查询参数
     * @return 结果
     */
    @Mapping(value = "/exist", method = MethodType.GET)
    public R<Boolean> exist(@Body TbyCameraInfoQuery query) {
        return this.tbyUavInfoService.checkCode(query.getId(), query.getCode());
    }

    /**
     * 验证无人机编码是否存在
     * @param query: 查询参数
     * @return 结果
     */
    @Mapping(value = "/device_exist", method = MethodType.GET)
    public R<Boolean> deviceExist(@Body TbyCameraInfoQuery query) {
        return this.tbyUavInfoService.checkDeviceId(query.getId(), query.getDeviceId());
    }

}
